﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace PjMediaStarter
{
    class Program
    {
        static int Main(string[] args)
        {

            TcpClient socketClient;
            NetworkStream networkStream;
            System.IO.StreamWriter streamWriter;
            System.IO.StreamReader streamReader;

            //Start socket with main Cryptophone2 program

            socketClient = new TcpClient("127.0.0.1", 3667);

            networkStream = socketClient.GetStream();
            streamWriter = new System.IO.StreamWriter(networkStream);
            streamReader = new System.IO.StreamReader(networkStream);

            string buffer = "";

            //Read ip 
            buffer = streamReader.ReadLine();
            StringBuilder url = new StringBuilder();
            url.Append(buffer);

            //Read keyTX
            buffer = streamReader.ReadLine();
            StringBuilder keyTX = new StringBuilder();
            keyTX.Append(buffer);

            //Read keyRX
            buffer = streamReader.ReadLine();
            StringBuilder keyRX = new StringBuilder();
            keyRX.Append(buffer);

            //Setting codec and port
            StringBuilder porta = new StringBuilder();
            porta.Append("3700");
            StringBuilder codec = new StringBuilder();
            codec.Append("SPEEX/8000/1");

            // Start pjmedia
            int result = PjMediaStarter.MyPjMedia.StartPjMedia(url, porta, codec, keyTX, keyRX);

            //Send call result
            buffer = result.ToString();

            streamWriter.WriteLine(buffer);
            streamWriter.Flush();

            //Read for quit command
            buffer = streamReader.ReadLine();
            int ret = 1;

            if (buffer.Equals("QUIT"))
                ret = 0;


            //Release of resources
            streamReader.Close();
            streamWriter.Close();
            networkStream.Close();
            socketClient.Close();

            return ret;           
        }
    }
}
